package com.qianzhou.eduCourseBoot.controller;

import com.qianzhou.eduCommon.constant.Constants;
import com.qianzhou.eduCommon.dto.CourseDTO;
import com.qianzhou.eduCommon.dto.CoursePageQueryDTO;
import com.qianzhou.eduCommon.po.Course;
import com.qianzhou.eduCommon.vo.CourseVO;
import com.qianzhou.eduCommon.vo.PageInfoVO;
import com.qianzhou.eduCommon.vo.ResultVO;
import com.qianzhou.eduCourseBoot.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * @Author: qian.zhou
     * @Description: 根据搜索条件查询课程信息
     * @DateTime: 2021/10/10 17:32
     * @Params: * @Param dto:
     * @Return: * @return: com.qianzhou.eduCommon.vo.ResultVO
     */
    @RequestMapping("/findCourseByCondition")
    public ResultVO findCourseByCondition(@RequestBody CoursePageQueryDTO dto) {
        PageInfoVO<Course> coursePageInfo = courseService.findCourseByCondition(dto);
        return ResultVO.ok(Constants.SUCCESS_STATUS, Constants.SUCCESS, coursePageInfo);
    }
    /**
    * @Author: qian.zhou
    * @Description: 新建课程和编辑课程
    * @DateTime: 2021/10/10 17:40
    * @Params: * @Param dto:
    * @Return: * @return: com.qianzhou.eduCommon.vo.ResultVO
    */
    @RequestMapping("/saveOrUpdateCourse")
    public ResultVO saveOrUpdateCourse(@RequestBody CourseDTO dto) {
        if (dto.getId() == null) {
            Integer integer = courseService.saveCourseOrTeacher(dto);
            if (integer == 2) {
                return ResultVO.ok(Constants.SUCCESS_STATUS,Constants.INSERT_SUCCESS,null);
            } else {
                return ResultVO.fail(Constants.FAILURE_STATUS,Constants.INSERT_FAILURE,null);
            }
        } else {
            Integer integer = courseService.updateCourseOrTeacher(dto);
            if (integer == 2) {
                return ResultVO.ok(Constants.SUCCESS_STATUS,Constants.UPDATE_SUCCESS,null);
            } else {
                return ResultVO.fail(Constants.FAILURE_STATUS,Constants.UPDATE_FAILURE,null);
            }
        }
    }
    /**
    * @Author: qian.zhou
    * @Description:
    * @DateTime: 2021/10/10 19:05
    * @Params: * @Param dto:
    * @Return: * @return: com.qianzhou.eduCommon.vo.ResultVO
    */
    @RequestMapping("/updateCourseStatus")
    public ResultVO updateCourseStatus(@RequestBody CourseDTO dto) {
        Integer integer = courseService.updateCourseStatus(dto);
        if (integer == 1) {
            return ResultVO.ok(Constants.SUCCESS_STATUS,Constants.UPDATE_STATUS_SUCCESS,null);
        } else {
            return ResultVO.fail(Constants.FAILURE_STATUS,Constants.UPDATE_STATUS_FAILURE,null);
        }
    }
    /**
    * @Author: qian.zhou
    * @Description: 编辑时的回显操作 其实就是简单的回显
    * @DateTime: 2021/10/10 19:15
    * @Params: * @Param id:
    * @Return: * @return: ResponseResultVO
    */
    @RequestMapping("/findCourseById/{id}")
    public ResultVO findCourseById(@PathVariable Integer id) {
        CourseVO vo = courseService.findCourseById(id);
        return ResultVO.ok(Constants.SUCCESS_STATUS,Constants.SUCCESS,vo);
    }

}
